
//力扣98. 验证二叉搜索树
 class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
     TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
          this.left = left;
          this.right = right;
      }
  }

class Solution {
    public boolean isValidBST(TreeNode root) {
        return isValid(root, null, null);
    }

    private boolean isValid(TreeNode node, Integer lower, Integer upper) {
        if (node == null) return true;

        // 检查当前节点是否在 (lower, upper) 范围内
        if (lower != null && node.val <= lower) return false;
        if (upper != null && node.val >= upper) return false;

        // 递归检查左子树（上界变为当前节点值）
        // 递归检查右子树（下界变为当前节点值）
        return isValid(node.left, lower, node.val)
                && isValid(node.right, node.val, upper);
    }
}